#include "SelectPictureItem.h"
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QImageReader>
#include <QRadialGradient>
#include <QLinearGradient>
#include "mainwindow.h"

SelectPictureItem::SelectPictureItem(const QString &path, QGraphicsItem *parent):
    mImage(0),
    mPath(path),
    mScale(1),
    mIsScale(false),
    mOffset(0,0),
    mGradient(0)
{
    // Read image
    QImageReader* reader = new QImageReader();
    // Set image name
    reader->setFileName(mPath);
    // Set/scale image size into screen
    QSize imageSize = reader->size();
    // Read image
    mImage = new QImage();
    if(false == reader->read(mImage))
    {
        delete reader;
        // Read image
        reader = new QImageReader();
        // Set image name
        reader->setFileName(mPath);
        QSize tSize = imageSize;
        tSize.scale(imageSize*0.8, Qt::KeepAspectRatio);
        reader->setScaledSize(tSize);
        if(false == reader->read(mImage))
        {
            delete reader;
            // Read image
            reader = new QImageReader();
            // Set image name
            reader->setFileName(mPath);
            QSize tSize = imageSize;
            tSize.scale(imageSize*0.5, Qt::KeepAspectRatio);
            reader->setScaledSize(tSize);
            if(false == reader->read(mImage))
            {
                delete reader;
                // Read image
                reader = new QImageReader();
                // Set image name
                reader->setFileName(mPath);
                QSize tSize = imageSize;
                tSize.scale(imageSize*0.25, Qt::KeepAspectRatio);
                qDebug()<<tSize.width()<<","<<tSize.height();
                reader->setScaledSize(tSize);
                reader->read(mImage);
            }
        }
    }
    delete reader;
}

SelectPictureItem::~SelectPictureItem()
{
    if(mImage)
        delete mImage;
}

void SelectPictureItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
    if(mImage)
    {
        //painter->setRenderHints(QPainter::Antialiasing|QPainter::SmoothPixmapTransform, true);
        QRectF rt = rect();
        QRect rtPix = mImage->rect();
        QSize imageSize = mImage->size();
        if(rt.width() > mImage->width() && rt.height() > mImage->height())
        {
            imageSize.scale(mImage->size()*mScale, Qt::KeepAspectRatio);
        }
        else
        {
            imageSize.scale(rt.size().toSize()*mScale, Qt::KeepAspectRatio);
        }

        rtPix.setSize(imageSize);

        painter->setPen(Qt::NoPen);
        if(!mGradient)
        {
            if(MainWindow::getViewMode() == 0)
            {
                mGradient = new QRadialGradient(rect().width()/2, rect().height()/2,
                                         rect().height()/2,
                                         rect().width()/2, rect().height()/2);
                mGradient->setColorAt(1, QColor::fromRgbF(0, 0, 0, 1));
                mGradient->setColorAt(0, QColor::fromRgbF(1, 0, 0, 1));
            }
            else
            {
                mGradient = new QLinearGradient(rect().width()/2,0,rect().width()/2,rect().height());
                mGradient->setColorAt(0, QColor::fromRgbF(0, 0, 0, 1));
                mGradient->setColorAt(0.5, QColor::fromRgbF(0.4, 0, 0, 1));
                mGradient->setColorAt(1, QColor::fromRgbF(0, 0, 0, 1));
            }
        }
        painter->setBrush(QBrush(*mGradient));

        QPainterPath path;
        path.addRect(rt);
        painter->drawPath(path);

        QPoint pt(0,0);


        pt.setX((rt.width()-rtPix.width())/2 + mOffset.x());

        pt.setY((rt.height()-rtPix.height())/2 + mOffset.y());

        rtPix.setTopLeft(pt);
        rtPix.setSize(imageSize);
        painter->drawImage(rtPix, *mImage);
    }
}

void SelectPictureItem::mouseMoveEvent ( QGraphicsSceneMouseEvent * event )
{
    QPointF rpt = (event->scenePos() - event->lastScenePos());

    if(rpt.y() != 0 && mIsScale)
    {
        mScale += rpt.y()/100;
        update();
        if(mScale < 0.1)
        {
            mScale = 0.1;
        }
    }
    if(!mIsScale)
    {
        mOffset += rpt;
        update();
    }
}
void SelectPictureItem::mousePressEvent ( QGraphicsSceneMouseEvent * event )
{
    mPressedPos = event->scenePos();
}
void SelectPictureItem::mouseReleaseEvent ( QGraphicsSceneMouseEvent * event )
{
    mReleasedPos = event->scenePos();
    QPointF rpt = (mReleasedPos - mPressedPos);
}
void SelectPictureItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
}
